import request from "../../common/utils/request";

const owner = {
  state: {
    ownerList: [],
  },
  mutations: {
    SET_OWNER_LIST: (state, list) => {
      state.ownerList = list;
    },
  },
  actions: {
    GetOwnerList: ({ state, commit }) => {
      return new Promise((resolve) => {
        if (state.ownerList.length) {
          resolve(state.ownerList);
        } else {
          request.get("/org", "/owner/manage/all").then((res) => {
            const result = res.list.map((item) => ({
              value: item.id,
              label: item.name,
            }));
            commit("SET_OWNER_LIST", result);
            resolve(result);
          });
        }
      });
    },
    GetOwnerUserList: ({ state, commit }, params) => {
      return new Promise((resolve) => {
        request.get("/village", "/org/owner/user/list", params).then((res) => {
          const result = res.list.map((item) => ({
            value: item.id,
            label: item.name,
          }));
          resolve(result);
        });
      });
    },
  },
};
export default owner;
